//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package org.quickserver.net.qsadmin.gui;

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Iterator;
import java.util.List;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;

public class MainCommandPanel extends JPanel {
    private QSAdminMain qsadminMain;
    private JPanel targetPanel;
    private JPanel commandPanel;
    private JLabel targetLabel;
    private JRadioButton serverButton;
    private JRadioButton selfButton;
    private String target = "server";
    private SimpleCommandSet sms;
    private GridBagConstraints gbc;

    public MainCommandPanel(QSAdminMain qsadminMain) {
        this.qsadminMain = qsadminMain;
        this.targetPanel = new JPanel();
        this.targetLabel = new JLabel("<html><font style=\"font-size:10pt;color:#535353\"> <b>Target : </b></font>", 2);
        this.serverButton = new JRadioButton("Server");
        this.serverButton.setMnemonic(83);
        this.serverButton.setActionCommand("server");
        this.serverButton.setSelected(true);
        this.selfButton = new JRadioButton("Admin Server");
        this.selfButton.setMnemonic(65);
        this.selfButton.setActionCommand("self");
        ButtonGroup group = new ButtonGroup();
        group.add(this.serverButton);
        group.add(this.selfButton);

        class RadioListener implements ActionListener {
            RadioListener() {
            }

            public void actionPerformed(ActionEvent e) {
                MainCommandPanel.this.setTarget(e.getActionCommand());
            }
        }

        RadioListener rListener = new RadioListener();
        this.serverButton.addActionListener(rListener);
        this.selfButton.addActionListener(rListener);
        this.targetPanel.add(this.targetLabel);
        this.targetPanel.add(this.serverButton);
        this.targetPanel.add(this.selfButton);
        this.sms = SimpleCommandSet.getSimpleCommands();
        List list = this.sms.getList();
        this.commandPanel = new JPanel();
        this.commandPanel.setLayout(new GridLayout(list.size(), 1, 2, 2));
        Iterator cmdIt = list.iterator();
        SimpleCommand sm = null;
        JButton cmdButton = null;

        while(cmdIt.hasNext()) {
            sm = (SimpleCommand)cmdIt.next();
            StringBuilder sf = new StringBuilder(sm.getName());
            if (sf.length() < 15) {
                sf.append(' ');
                sf.insert(0, ' ');
            }

            cmdButton = new JButton("<html><font style=\"font-size:10pt;color:#008080\"><b>" + sf.toString() + "</b></font>");
            cmdButton.setToolTipText(sm.getDesc());
            cmdButton.addActionListener(this.getSimpleAction(sm));
            this.commandPanel.add(cmdButton);
        }

        this.setLayout(new BorderLayout(0, 10));
        this.add(this.targetPanel, "North");
        JScrollPane commandScrollPane = new JScrollPane(this.commandPanel);
        this.add(commandScrollPane, "Center");
    }

    public String getTarget() {
        return this.target;
    }

    private void setTarget(String target) {
        this.target = target;
    }

    private ActionListener getSimpleAction(SimpleCommand cm) {
        return new SimpleActionListener(this.qsadminMain, cm, this);
    }

    public void updateConnectionStatus(boolean connected) {
        this.serverButton.setEnabled(connected);
        this.selfButton.setEnabled(connected);
        this.commandPanel.setEnabled(connected);
    }
}
